package com.xuanrui.config;


import com.huifu.adapay.Adapay;
import com.huifu.adapay.model.MerConfig;
import com.xuanrui.config.adapay.AdapayListConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sunke
 * @version 1.0
 * @Description
 * @date 2022-09-20 15:55
 */
@Configuration
@EnableConfigurationProperties({AdapayListConfig.class})
public class BeanConfig {
    @Autowired
    private AdapayListConfig adapayListConfig;
    @Bean
    public AdapayListConfig adapayListConfig() throws Exception {
        return adapayListConfig;
    }
    @Bean
    public Map<String, MerConfig> merConfigs() throws Exception {
        Map<String, MerConfig> configMap = new HashMap<>();

        adapayListConfig.getList().forEach(adapayItemConfig -> {
            MerConfig merConfig = new MerConfig();
            merConfig.setApiKey(adapayItemConfig.getApiKey());
            merConfig.setApiMockKey(adapayItemConfig.getMockApiKey());
            merConfig.setRSAPrivateKey(adapayItemConfig.getRsaPrivateKey());
            merConfig.setRSAPublicKey(adapayItemConfig.getRsaPublicKey());
            configMap.put(adapayItemConfig.getApiKey(), merConfig);
        });
        Adapay.initWithMerConfigs(configMap);
        return configMap;
    }
}
